<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="108px">
      <el-form-item label="配送店名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入配送店名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配送店电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入配送店电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:shop:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:shop:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:shop:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:shop:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="配送店编号" align="center" prop="id" />
      <el-table-column label="配送店名称" align="center" prop="name" />
      <el-table-column label="配送店电话" align="center" prop="phone" />
<!--      <el-table-column label="省" align="center" prop="provinceName" />-->
<!--      <el-table-column label="省code" align="center" prop="provinceCode" />-->
<!--      <el-table-column label="市" align="center" prop="cityName" />-->
<!--      <el-table-column label="市code" align="center" prop="cityCode" />-->
<!--      <el-table-column label="区" align="center" prop="districtName" />-->
<!--      <el-table-column label="区code" align="center" prop="districtCode" />-->
      <el-table-column label="地址标签" align="center" prop="addressTag" >
        <template slot-scope="scope">
          <el-tag type="success">{{ shopTypeText( scope.row.addressTag)}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="详细地址" align="center" prop="detailAddress">
        <template slot-scope="scope">
          <span>{{ scope.row.provinceName+' '+scope.row.cityName+' '+scope.row.districtName+' '+scope.row.detailAddress }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status|statusType">{{ scope.row.status|statusText }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:shop:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:shop:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改配送店对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="配送店名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入配送店名称" />
        </el-form-item>
        <el-form-item label="配送店电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入配送店电话" />
        </el-form-item>
        <el-form-item prop="addressCode" label="店铺地址">
          <el-cascader
            v-model="form.addressCode"
            :options="provinces"
            class="filter-item full"
            placeholder="店铺地址"
          />
        </el-form-item>
        <el-form-item prop="detailAddress" label="详细地址">
          <el-input
            v-model="form.detailAddress"
            style="margin-top: 5px;"
            type="textarea"
            placeholder="详细地址"
          />
        </el-form-item>
<!--        <el-form-item label="省" prop="provinceName">-->
<!--          <el-input v-model="form.provinceName" placeholder="请输入省" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="省code" prop="provinceCode">-->
<!--          <el-input v-model="form.provinceCode" placeholder="请输入省code" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="市" prop="cityName">-->
<!--          <el-input v-model="form.cityName" placeholder="请输入市" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="市code" prop="cityCode">-->
<!--          <el-input v-model="form.cityCode" placeholder="请输入市code" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="区" prop="districtName">-->
<!--          <el-input v-model="form.districtName" placeholder="请输入区" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="区code" prop="districtCode">-->
<!--          <el-input v-model="form.districtCode" placeholder="请输入区code" />-->
<!--        </el-form-item>-->
        <el-form-item label="地址标签" prop="addressTag">
<!--          <el-input v-model="form.addressTag" placeholder="请输入地址标签" />-->
                <el-select v-model="form.addressTag" placeholder="请选择">
                  <el-option
                    v-for="item in shopType"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue">
                  </el-option>
                </el-select>
        </el-form-item>
<!--        <el-form-item label="详细地址" prop="detailAddress">-->
<!--          <el-input v-model="form.detailAddress" placeholder="请输入详细地址" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShop, getShop, delShop, addShop, updateShop } from "@/api/system/shop"
import { regionData,codeToText } from 'element-china-area-data'
import {getAvatar, getAvatarList, getStatus,DICT_TYPE} from "../../../utils/jpUtils";
import {getDicts} from "../../../api/system/dict/data";
export default {
  name: "Shop",
  data() {
    return {
      shopTypeDict:new Map(),
      shopType:[],
      provinces: regionData,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 配送店表格数据
      shopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        phone: null,
        provinceName: null,
        provinceCode: null,
        cityName: null,
        cityCode: null,
        districtName: null,
        districtCode: null,
        addressTag: null,
        detailAddress: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "店铺名称不能为空", trigger: "blur"}
        ],
        phone: [
          {required: true, message: "联系电话不能为空", trigger: "blur"}
        ],
        addressCode: [
          {required: true, message: "店铺地址不能为空", trigger: "blur"}
        ],
        detailAddress: [
          {required: true, message: "详细地址不能为空", trigger: "blur"}
        ],
        addressTag: [
          {required: true, message: "地址标签不能为空", trigger: "blur"}
        ]
      }
    }
  },
  created() {
    this.initDic()
    this.getList()
  }, filters: {

    statusText(val) {
      //0正常 1停用 2已经使用）
      return getStatus(val).txt
    },
    statusType(val) {
      return getStatus(val).type
    },
    getAvatarList,
    getAvatar
  },
  methods: {
    shopTypeText(val){
      return this.shopTypeDict.get(val)
    },
    initDic(){
      getDicts(DICT_TYPE.SHOP_TYPE).then(res=>{
        this.shopType = res.data
        if(this.shopType){
          let map = new Map();
           this.shopType.forEach(item=>{
             map.set(item.dictValue,item.dictLabel)
           })
          this.shopTypeDict = map;
        }
      })
    },
    /** 查询配送店列表 */
    getList() {
      this.loading = true
      listShop(this.queryParams).then(response => {
        this.shopList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        phone: null,
        addressCode:null,
        provinceName: null,
        provinceCode: null,
        cityName: null,
        cityCode: null,
        districtName: null,
        districtCode: null,
        addressTag: null,
        detailAddress: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加配送店"
      console.log(this.provinces)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getShop(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改配送店"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let addrssCode = this.form.addressCode
          let provinceCode = addrssCode[0]
          let cityCode = addrssCode[1]
          let districtCode = addrssCode[2]

          let provinceName = codeToText[provinceCode]
          let cityName = codeToText[cityCode]
          let districtName = codeToText[districtCode]

          this.form.provinceCode = provinceCode
          this.form.cityCode = cityCode
          this.form.districtCode = districtCode

          this.form.provinceName = provinceName
          this.form.cityName = cityName
          this.form.districtName = districtName

          if (this.form.id != null) {
            updateShop(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addShop(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除配送店["' + row.name + '"]？').then(function() {
        return delShop(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/shop/export', {
        ...this.queryParams
      }, `shop_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
